const { body } = require('express-validator')
const validate = require('../middleware/validate')
const { User } = require('../model')

exports.message = validate([
  body('user.phone')
    .notEmpty().withMessage('手机号不能为空')
    .custom(async phone => {
      const user = await User.findOne({ phone })
      if (user) {
        return Promise.reject('手机号已存在')
      }
    })
])

exports.loginMessage = validate([
  body('user.phone')
    .notEmpty().withMessage('手机号不能为空')
    .custom(async phone => {
      const user = await User.findOne({ phone })
      if (!user) {
        return Promise.reject('手机号不存在')
      }
    })
])

